﻿using System;
using System.Collections.Generic;
using System.IO;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKCoordinate
{
	public class FamilyCreater
	{
		private FamilyCreater()
		{
		}

		public static FamilyCreater GetFamilyCreater()
		{
			return FamilyCreater.singleTag;
		}

		public string SetResultData(UIApplication ui_app, BoundryCurvePakcer input, double deep)
		{
			this.reSet();
			this.m_uiapp = ui_app;
			this.m_ddeep = deep;
			this.m_boundryCurvePacker = input;
			this.creatTransform();
			this.creatFamily();
			return this.m_strOutPutPath;
		}

		private void reSet()
		{
			this.m_uiapp = null;
			this.m_ddeep = 0.0;
			this.m_transFormdriection = null;
			this.m_startPoint = null;
			this.m_endPoint = null;
			if (this.m_lstBoundryCurve == null)
			{
				this.m_lstBoundryCurve = new List<Curve>();
			}
			else
			{
				this.m_lstBoundryCurve.Clear();
			}
			this.m_strOutPutPath = null;
		}

		public XYZ GetRealStartPoint()
		{
			return this.m_boundryCurvePacker.GetPoint(0);
		}

		private void creatTransform()
		{
			this.m_startPoint = XYZ.Zero;
			this.m_transFormdriection = this.m_startPoint.Add(-this.m_boundryCurvePacker.GetPoint(0));
			this.m_transForm = Transform.CreateTranslation(this.m_transFormdriection);
			this.m_endPoint = this.m_boundryCurvePacker.GetPoint(1).Add(this.m_transFormdriection);
			foreach (Curve curve in this.m_boundryCurvePacker.LstBoundryCurves)
			{
				this.m_lstBoundryCurve.Add(curve.CreateTransformed(this.m_transForm));
			}
		}

		private void creatFamily()
		{
			if (!File.Exists(StaticRescource.StrPathFamilyTemplate))
			{
				this.m_strOutPutPath = null;
				return;
			}
			Document document = this.m_uiapp.Application.NewFamilyDocument(StaticRescource.StrPathFamilyTemplate);
			if (!document.IsFamilyDocument)
			{
				this.m_strOutPutPath = null;
				return;
			}
			FamilyManager familyManager = document.FamilyManager;
			Application application = document.Application;
			Transaction transaction = new Transaction(document, "creatFamily");
			try
			{
				transaction.Start();
				CurveArrArray curveArrArray = application.Create.NewCurveArrArray();
				CurveArray curveArray = application.Create.NewCurveArray();
				foreach (Curve curve in this.m_lstBoundryCurve)
				{
					curveArray.Append(curve);
				}
				curveArrArray.Append(curveArray);
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(new XYZ(0.0, 0.0, 1.0), new XYZ(0.0, 0.0, 0.0));
				SketchPlane sketchPlane = SketchPlane.Create(document, plane);
				document.FamilyCreate.NewExtrusion(true, curveArrArray, sketchPlane, -this.m_ddeep);
				transaction.Commit();
				SaveAsOptions saveAsOptions = new SaveAsOptions();
				saveAsOptions.OverwriteExistingFile = true;
				this.m_strOutPutPath = StaticRescource.StrPathOutPut + StaticRescource.GetADateTime() + this.m_noutPutInex.ToString() + ".rfa";
				document.SaveAs(this.m_strOutPutPath, saveAsOptions);
				document.Close();
				this.m_noutPutInex++;
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}

		private UIApplication m_uiapp;

		private double m_ddeep;

		private BoundryCurvePakcer m_boundryCurvePacker;

		private XYZ m_transFormdriection;

		private Transform m_transForm;

		private XYZ m_startPoint;

		private XYZ m_endPoint;

		private List<Curve> m_lstBoundryCurve;

		private int m_noutPutInex;

		private string m_strOutPutPath;

		private static FamilyCreater singleTag = new FamilyCreater();
	}
}
